package com.hygj.controller;

import com.hygj.pojo.Role;
import com.hygj.pojo.User;
import com.hygj.service.RoleService;
import com.hygj.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 *
 * @author hygl
 */
@Controller
@RequestMapping
public class RoleController {

    @Autowired
    private RoleService roleService;

    static Map<String, String> roles = new HashMap<String, String>();

    /**
     * 角色添加
     *
     * @return
     */
    @RequestMapping("insertRole")
    public String insertRole(Role role) {
        roleService.insert(role);
        return "redirect:getRoleAll";
    }

    /**
     * 查询全部角色(列表用)
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("getRoleAll")
    public String getLoginAll(@ModelAttribute("search") Role role, Map<String, Object> map,
                              HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);
        // 第一步：判断长度，是否为空
        if (StringUtils.isNotBlank(role.getRole_name())) {
            params.put("role_name", "%" + role.getRole_name() + "%");
        }
        PageUtil<Role> page = roleService.page(request, 10, params);
        map.put("page", page);
        return "admin/admin_role/role_show";
    }

    /**
     * 查询角色---添加用户使用
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("getRole")
    public String getRoleAll(Map<String, Object> map, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<Role> page = roleService.listAll("listAll", params);
        // list转换成Map
        Map<String, String> _r = new HashMap<String, String>();
        for (Role role : page) {
            _r.put("" + role.getRid(), role.getRole_name());
        }
        map.put("roles", _r);
        // springmvc 使用<from:from>时必须得绑定参数
        map.put("user", new User());
        return "admin/admin_user/user_add";
    }

    /**
     * 回显角色信息(根据‘rid’)
     *
     * @param rid
     * @param modelMap
     * @return
     */
    @RequestMapping("getRoleById")
    public String getRoleById(@RequestParam("inform") Integer rid, ModelMap modelMap) {
        Role roles = roleService.getById("getById", rid);
        modelMap.addAttribute("roles", roles);
        return "admin/admin_role/role_update";
    }

    /**
     * 角色信息修改
     *
     * @param role
     * @return
     */
    @RequestMapping("updateRole")
    public String updateRole(Role role) {
        roleService.update("update", role);
        return "redirect:getRoleAll";
    }


    /**
     * 删除，多项删除 @RequestParam("inform")引入 选择框 inform
     * ===><input type="checkbox" name="inform">
     * @return
     */
    @RequestMapping("delete_role")
    public String deleteRole(@RequestParam("inform") Integer[] ids){
        roleService.delete(ids);
        return "redirect:getRoleAll";
    }

}
